//
//  HMSportTrackController.m
//  HMSport
//
//  Created by apple on 2017/4/23.
//  Copyright © 2017年 itheima. All rights reserved.
//

#import "HMSportTrackController.h"
#import "HMSportMapViewController.h"
#import "HMAnimator.h"
#import "HMSportMapModeController.h"

@interface HMSportTrackController ()<UIPopoverPresentationControllerDelegate>

@property (nonatomic, strong) HMSportMapViewController *mapVc;

@property (nonatomic, strong) HMAnimator *animator;
@property (weak, nonatomic) IBOutlet UILabel *distanceLabel;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;

@end

@implementation HMSportTrackController

- (instancetype)init
{
    self = [super init];
    if (self) {
        
        //设置地图
        [self setupMapView];
        //设置自定义展示样式
        self.modalPresentationStyle = UIModalPresentationCustom;
        //强引用动画器
        self.animator = [HMAnimator new];
        self.transitioningDelegate = self.animator;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(locationDidUpdateNote) name:@"HMSportLocationDidUpdateNote" object:nil];
    //设置字体 字体名称需要使用PostScript Name
    self.timeLabel.font = [UIFont fontWithName:@"DINCond-Bold" size:30];
}

//位置更新后响应
- (void)locationDidUpdateNote{

    //设置数据
    self.distanceLabel.text = [NSString stringWithFormat:@"%.2f", self.track.totalDistance];
    int time = (int) self.track.totalTime;
    self.timeLabel.text = [NSString stringWithFormat:@"%02d:%02d:%02d", time / 3600, (time % 3600) / 60, time % 60];
}

- (void)setupMapView{
    
    //使用XIB时,在ViewDidLoad中的视图尺寸和XIB中预览的尺寸一致(视图的尺寸还没有同步设备的真实尺寸)
    
    //创建mapVc
    HMSportMapViewController *mapVc = [[HMSportMapViewController alloc] init];
    //使用父子控制器的方式
    //1.添加视图控制器(不添加也可以显示,但是响应者链条断开)    SB添加视图控制器使用ContainerView
    [self addChildViewController:mapVc];
    //2.添加内容视图为子视图
    [self.view insertSubview:mapVc.view atIndex:0];
    //3.设置子视图的尺寸 添加子视图时一定要设置其尺寸,避免出现autoresizing的错误处理
    mapVc.view.frame = self.view.bounds;
    //官方推荐
    [mapVc didMoveToParentViewController:self];
    self.mapVc = mapVc;
}



#pragma mark - 响应事件

//点击关闭
- (IBAction)clickCloseBtn:(id)sender {
   [self dismissViewControllerAnimated:YES completion:nil];
}

//点击地图类型
- (IBAction)clickMapModeBtn:(UIButton *)sender {
    //Popover 弹出窗  属于modal的一种
    //创建地图类型控制器
    HMSportMapModeController *mapModeVc = [[HMSportMapModeController alloc] initWithSelectBlock:^(HMMapType mapType) {
        //传递地图类型给mapVc
        self.mapVc.mapType = mapType;
    }];
    //将当前地图类型传递给mapMode
    mapModeVc.mapType = self.mapVc.mapType;
    //modal展示样式  iOS会自动检测设备类型,如果是iPhone上使用popover,还是会使用默认的modal样式进行适配
    mapModeVc.modalPresentationStyle = UIModalPresentationPopover;

    //iPhone使用Popover的解决办法: 取消系统的默认适配
    //获取Popover对象 用于设置Popover相关的数据
    UIPopoverPresentationController *popover = mapModeVc.popoverPresentationController;
    //设置Popover的代理  取消Modal的设备适配
    popover.delegate = self;
    //设置来源视图/BarButtonItem  展示Popover之前,必须设置参考点(参考的控件)
    popover.sourceView = sender;
    //设置Popover的大小 preferredContentSize只能用于修改进行Popover展示的控制器的视图的大小
    mapModeVc.preferredContentSize = CGSizeMake(300, 120);
    //设置箭头方向
    popover.permittedArrowDirections = UIPopoverArrowDirectionDown;
    //进行展示
    [self presentViewController:mapModeVc animated:YES completion:nil];
}

#pragma mark - setter & getter

- (void)setTrack:(HMSportTrack *)track{

    _track = track;
    //传递轨迹模型
    self.mapVc.track = track;
}

- (void)setTrackBtnCenter:(CGPoint)trackBtnCenter{
    
    _trackBtnCenter = trackBtnCenter;
    self.mapVc.trackBtnCenter = _trackBtnCenter;
}


- (void)dealloc{

    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


#pragma mark - UIAdaptivePresentationControllerDelegate 
//注意取消Modal自适应需要实现UIPopoverPresentationControllerDelegate的基协议中的方法

//对Modal样式进行自适应时调用
- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller{
    //取消适配
    return UIModalPresentationNone;
}

@end
